/
Executor.h
351 lines (289 loc) · 11.2 KB
/
Executor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <cassert>
#include <climits>
#include <utility>
#include <folly/Function.h>
#include <folly/Optional.h>
#include <folly/Range.h>
#include <folly/Utility.h>
#include <folly/lang/Exception.h>
namespace folly {
using Func = Function<void()>;
namespace detail {
class ExecutorKeepAliveBase {
public:
// A dummy keep-alive is a keep-alive to an executor which does not support
// the keep-alive mechanism.
static constexpr uintptr_t kDummyFlag = uintptr_t(1) << 0;
// An alias keep-alive is a keep-alive to an executor to which there is
// known to be another keep-alive whose lifetime surrounds the lifetime of
// the alias.
static constexpr uintptr_t kAliasFlag = uintptr_t(1) << 1;
static constexpr uintptr_t kFlagMask = kDummyFlag | kAliasFlag;
static constexpr uintptr_t kExecutorMask = ~kFlagMask;
};
} // namespace detail
/// An Executor accepts units of work with add(), which should be
/// threadsafe.
class Executor {
public:
virtual ~Executor() = default;
/// Enqueue a function to be executed by this executor. This and all
/// variants must be threadsafe.
virtual void add(Func) = 0;
/// Enqueue a function with a given priority, where 0 is the medium priority
/// This is up to the implementation to enforce
virtual void addWithPriority(Func, int8_t priority);
virtual uint8_t getNumPriorities() const { return 1; }
static constexpr int8_t LO_PRI = SCHAR_MIN;
static constexpr int8_t MID_PRI = 0;
static constexpr int8_t HI_PRI = SCHAR_MAX;
/**
* Executor::KeepAlive is a safe pointer to an Executor.
* For any Executor that supports KeepAlive functionality, Executor's
* destructor will block until all the KeepAlive objects associated with that
* Executor are destroyed.
* For Executors that don't support the KeepAlive functionality, KeepAlive
* doesn't provide such protection.
*
* KeepAlive should *always* be used instead of Executor*. KeepAlive can be
* implicitly constructed from Executor*. getKeepAliveToken() helper method
* can be used to construct a KeepAlive in templated code if you need to
* preserve the original Executor type.
*/
template <typename ExecutorT = Executor>
class KeepAlive : private detail::ExecutorKeepAliveBase {
public:
using KeepAliveFunc = Function<void(KeepAlive&&)>;
KeepAlive() = default;
~KeepAlive() {
static_assert(
std::is_standard_layout<KeepAlive>::value, "standard-layout");
static_assert(sizeof(KeepAlive) == sizeof(void*), "pointer size");
static_assert(alignof(KeepAlive) == alignof(void*), "pointer align");
reset();
}
KeepAlive(KeepAlive&& other) noexcept
: storage_(std::exchange(other.storage_, 0)) {}
KeepAlive(const KeepAlive& other) noexcept
: KeepAlive(getKeepAliveToken(other.get())) {}
template <
typename OtherExecutor,
typename = typename std::enable_if<
std::is_convertible<OtherExecutor*, ExecutorT*>::value>::type>
/* implicit */ KeepAlive(KeepAlive<OtherExecutor>&& other) noexcept
: KeepAlive(other.get(), other.storage_ & kFlagMask) {
other.storage_ = 0;
}
template <
typename OtherExecutor,
typename = typename std::enable_if<
std::is_convertible<OtherExecutor*, ExecutorT*>::value>::type>
/* implicit */ KeepAlive(const KeepAlive<OtherExecutor>& other) noexcept
: KeepAlive(getKeepAliveToken(other.get())) {}
/* implicit */ KeepAlive(ExecutorT* executor) {
*this = getKeepAliveToken(executor);
}
KeepAlive& operator=(KeepAlive&& other) noexcept {
reset();
storage_ = std::exchange(other.storage_, 0);
return *this;
}
KeepAlive& operator=(KeepAlive const& other) {
return operator=(folly::copy(other));
}
template <
typename OtherExecutor,
typename = typename std::enable_if<
std::is_convertible<OtherExecutor*, ExecutorT*>::value>::type>
KeepAlive& operator=(KeepAlive<OtherExecutor>&& other) noexcept {
return *this = KeepAlive(std::move(other));
}
template <
typename OtherExecutor,
typename = typename std::enable_if<
std::is_convertible<OtherExecutor*, ExecutorT*>::value>::type>
KeepAlive& operator=(const KeepAlive<OtherExecutor>& other) {
return *this = KeepAlive(other);
}
void reset() noexcept {
if (Executor* executor = get()) {
auto const flags = std::exchange(storage_, 0) & kFlagMask;
if (!(flags & (kDummyFlag | kAliasFlag))) {
executor->keepAliveRelease();
}
}
}
explicit operator bool() const { return storage_; }
ExecutorT* get() const {
return reinterpret_cast<ExecutorT*>(storage_ & kExecutorMask);
}
ExecutorT& operator*() const { return *get(); }
ExecutorT* operator->() const { return get(); }
KeepAlive copy() const {
return isKeepAliveDummy(*this) //
? makeKeepAliveDummy(get())
: getKeepAliveToken(get());
}
KeepAlive get_alias() const { return KeepAlive(storage_ | kAliasFlag); }
template <class KAF>
void add(KAF&& f) && {
static_assert(
is_invocable<KAF, KeepAlive&&>::value,
"Parameter to add must be void(KeepAlive&&)>");
auto ex = get();
ex->add([ka = std::move(*this), f_2 = std::forward<KAF>(f)]() mutable {
f_2(std::move(ka));
});
}
private:
friend class Executor;
template <typename OtherExecutor>
friend class KeepAlive;
KeepAlive(ExecutorT* executor, uintptr_t flags) noexcept
: storage_(reinterpret_cast<uintptr_t>(executor) | flags) {
assert(executor);
assert(!(reinterpret_cast<uintptr_t>(executor) & ~kExecutorMask));
assert(!(flags & kExecutorMask));
}
explicit KeepAlive(uintptr_t storage) noexcept : storage_(storage) {}
// Combined storage for the executor pointer and for all flags.
uintptr_t storage_{reinterpret_cast<uintptr_t>(nullptr)};
};
template <typename ExecutorT>
static KeepAlive<ExecutorT> getKeepAliveToken(ExecutorT* executor) {
static_assert(
std::is_base_of<Executor, ExecutorT>::value,
"getKeepAliveToken only works for folly::Executor implementations.");
if (!executor) {
return {};
}
folly::Executor* executorPtr = executor;
if (executorPtr->keepAliveAcquire()) {
return makeKeepAlive<ExecutorT>(executor);
}
return makeKeepAliveDummy<ExecutorT>(executor);
}
template <typename ExecutorT>
static KeepAlive<ExecutorT> getKeepAliveToken(ExecutorT& executor) {
static_assert(
std::is_base_of<Executor, ExecutorT>::value,
"getKeepAliveToken only works for folly::Executor implementations.");
return getKeepAliveToken(&executor);
}
template <typename F>
FOLLY_ERASE static void invokeCatchingExns(char const* p, F f) noexcept {
catch_exception(f, invokeCatchingExnsLog, p);
}
protected:
/**
* Returns true if the KeepAlive is constructed from an executor that does
* not support the keep alive ref-counting functionality
*/
template <typename ExecutorT>
static bool isKeepAliveDummy(const KeepAlive<ExecutorT>& keepAlive) {
return keepAlive.storage_ & KeepAlive<ExecutorT>::kDummyFlag;
}
static bool keepAliveAcquire(Executor* executor) {
return executor->keepAliveAcquire();
}
static void keepAliveRelease(Executor* executor) {
return executor->keepAliveRelease();
}
// Acquire a keep alive token. Should return false if keep-alive mechanism
// is not supported.
virtual bool keepAliveAcquire() noexcept;
// Release a keep alive token previously acquired by keepAliveAcquire().
// Will never be called if keepAliveAcquire() returns false.
virtual void keepAliveRelease() noexcept;
template <typename ExecutorT>
static KeepAlive<ExecutorT> makeKeepAlive(ExecutorT* executor) {
static_assert(
std::is_base_of<Executor, ExecutorT>::value,
"makeKeepAlive only works for folly::Executor implementations.");
return KeepAlive<ExecutorT>{executor, uintptr_t(0)};
}
private:
static void invokeCatchingExnsLog(char const* prefix) noexcept;
template <typename ExecutorT>
static KeepAlive<ExecutorT> makeKeepAliveDummy(ExecutorT* executor) {
static_assert(
std::is_base_of<Executor, ExecutorT>::value,
"makeKeepAliveDummy only works for folly::Executor implementations.");
return KeepAlive<ExecutorT>{executor, KeepAlive<ExecutorT>::kDummyFlag};
}
};
/// Returns a keep-alive token which guarantees that Executor will keep
/// processing tasks until the token is released (if supported by Executor).
/// KeepAlive always contains a valid pointer to an Executor.
template <typename ExecutorT>
Executor::KeepAlive<ExecutorT> getKeepAliveToken(ExecutorT* executor) {
static_assert(
std::is_base_of<Executor, ExecutorT>::value,
"getKeepAliveToken only works for folly::Executor implementations.");
return Executor::getKeepAliveToken(executor);
}
template <typename ExecutorT>
Executor::KeepAlive<ExecutorT> getKeepAliveToken(ExecutorT& executor) {
static_assert(
std::is_base_of<Executor, ExecutorT>::value,
"getKeepAliveToken only works for folly::Executor implementations.");
return getKeepAliveToken(&executor);
}
template <typename ExecutorT>
Executor::KeepAlive<ExecutorT> getKeepAliveToken(
Executor::KeepAlive<ExecutorT>& ka) {
return ka.copy();
}
struct ExecutorBlockingContext {
bool forbid;
bool allowTerminationOnBlocking;
Executor* ex = nullptr;
StringPiece tag;
};
static_assert(
std::is_standard_layout<ExecutorBlockingContext>::value,
"non-standard layout");
struct ExecutorBlockingList {
ExecutorBlockingList* prev;
ExecutorBlockingContext curr;
};
static_assert(
std::is_standard_layout<ExecutorBlockingList>::value,
"non-standard layout");
class ExecutorBlockingGuard {
public:
struct PermitTag {};
struct TrackTag {};
struct ProhibitTag {};
~ExecutorBlockingGuard();
ExecutorBlockingGuard() = delete;
explicit ExecutorBlockingGuard(PermitTag) noexcept;
explicit ExecutorBlockingGuard(
TrackTag, Executor* ex, StringPiece tag) noexcept;
explicit ExecutorBlockingGuard(
ProhibitTag, Executor* ex, StringPiece tag) noexcept;
ExecutorBlockingGuard(ExecutorBlockingGuard&&) = delete;
ExecutorBlockingGuard(ExecutorBlockingGuard const&) = delete;
ExecutorBlockingGuard& operator=(ExecutorBlockingGuard const&) = delete;
ExecutorBlockingGuard& operator=(ExecutorBlockingGuard&&) = delete;
private:
ExecutorBlockingList list_;
};
Optional<ExecutorBlockingContext> getExecutorBlockingContext() noexcept;
} // namespace folly